package ch.cm.android.teamlearn.learnactivity;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import ch.cm.android.teamlearn.R;
import ch.cm.android.teamlearn.model.Question;
import ch.cm.android.teamlearn.orm.DataBaseHelper;

import com.j256.ormlite.dao.Dao;

public class LeanActivity extends Activity {

    private static final String TAG = LeanActivity.class.getName();

    private String tag = "";
    private ArrayList<Question> selectedQuestions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_learn);
        tag = getIntent().getExtras().getString("tag");
        selectedQuestions = new ArrayList<Question>();
        if (tag.equals("")) {
            Toast.makeText(this, "No Tag in " + TAG, Toast.LENGTH_SHORT).show();
            finish();
        } else {
            try {
                Dao<Question, Integer> dao = new DataBaseHelper(this).getDao();
                selectedQuestions.addAll(dao.queryForEq("tag", tag));
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        Question question = getRandomQuestion();
        displayQuestion(question);
    }

    private Question getRandomQuestion() {
        Random random = new Random();
        int nextInt = random.nextInt(selectedQuestions.size());
        Log.d(TAG, "nextint: "+ nextInt);
        return selectedQuestions.get(nextInt);
    }

    private void displayQuestion(Question question) {
        Log.d(TAG, "enter display Question - Private_ID: "+question.private_id);
        
        TextView textView = (TextView) findViewById(R.id.tv_activity_learn_question);
        Button button1 = (Button) findViewById(R.id.bu_activity_learn_b1);
        Button button2 = (Button) findViewById(R.id.bu_activity_learn_b2);
        Button button3 = (Button) findViewById(R.id.bu_activity_learn_b3);

        textView.setText(question.question);

        ArrayList<Button> buttons = new ArrayList<Button>();
        
        buttons.add(button1);
        buttons.add(button2);
        buttons.add(button3);
        
        Collections.shuffle(buttons);
        setupButtons(buttons,question);
    }

    private void setupButtons(ArrayList<Button> buttons,Question question) {
        for (Button button : buttons) {
            button.setEnabled(true);
        }
        
        buttons.get(0).setText(question.rightAnswer);
        buttons.get(0).setOnClickListener(new AnswerButttonListener(true));
        buttons.get(1).setText(question.wrongAnswer1);
        buttons.get(1).setOnClickListener(new AnswerButttonListener(false));
        buttons.get(2).setText(question.wrongAnswer2);
        buttons.get(2).setOnClickListener(new AnswerButttonListener(false));
        
    }

    private class AnswerButttonListener implements OnClickListener {
        
        Boolean isrightAnswer = false;
        private AnswerButttonListener(Boolean isrightAnswer) {
            this.isrightAnswer = isrightAnswer;
        }

        @Override
        public void onClick(View v) {
            if (!isrightAnswer) {
                v.setEnabled(false);
                Toast.makeText(LeanActivity.this, "Leider Nein", Toast.LENGTH_SHORT).show();
            }else {
                displayQuestion(getRandomQuestion());
                Toast.makeText(LeanActivity.this,"Richtig", Toast.LENGTH_SHORT).show();
            }

        }
    }

}
